<?php

namespace App\Admin\Controllers\Aliyun;

use App\Admin\Actions\Aliyun\UpdateWebVersion;
use App\Admin\Traits\WithUploads;
use App\Enums\BucketType;
use App\Models\Application;
use App\Models\Version;
use App\Models\VersionLog;
use Dcat\Admin\Admin;
use Dcat\Admin\Exception\InvalidArgumentException;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Layout\Content;

class VersionController extends AdminController
{
    use WithUploads;

    protected string $title = '版本管理';

    /**
     * @throws InvalidArgumentException
     */
    public function grid(): Grid
    {
        return Grid::make(Version::ofAdmin()->latest()->withCount(['logs']), function (Grid $grid) {
            $grid->disableBatchDelete(false);
            $grid->disableEditButton();
            $grid->enableDialogCreate();
            $grid->showQuickEditButton();
            $grid->actions([new UpdateWebVersion]);

            $grid->quickSearch(['version', 'remark'])
                ->placeholder('搜索版本号');

            if (Admin::user()->isAdministrator()) {
                $grid->column('application.name', '所属应用');
            }
            $grid->column('version', '版本名称');
            $grid->column('remark', '备注说明');
            $grid->column('type')
                ->using(BucketType::TYPE_MAP);
            $grid->column('logs_count', '发布次数')
                ->link(fn() => route('admin.versions.logs', $this), '_self');
            $grid->column('status')
                ->bool();
            $grid->column('created_at');
        });
    }

    /**
     * @throws InvalidArgumentException
     */
    public function form(): Form
    {
        return Form::make(Version::ofAdmin(), function (Form $form) {
            if (Admin::user()->isAdministrator()) {
                $form->select('application_id', '所属应用')
                    ->required()
                    ->options(Application::ofEnabled()->pluck('name', 'id'));
            }
            $form->text('version', '版本名称')
                ->required();
            $this->file($form, 'file', '程序ZIP包')
                ->required()
                ->accept('zip');
            $form->radio('type', '程序包类型')
                ->required()
                ->options(BucketType::VERSION_TYPES);
            $form->textarea('remark', '备注说明');
            $form->switch('status')
                ->default(true);
        });
    }

    /**
     * @throws InvalidArgumentException
     */
    public function logs(Content $content, Version $version): Content
    {
        return $content->header($version->version)
            ->description('发布记录')
            ->body($this->logsGrid($version));
    }

    /**
     * @throws InvalidArgumentException
     */
    public function logsGrid(Version $version)
    {
        $model = VersionLog::ofAdmin()
            ->where('version_id', $version->getKey())
            ->with(['objectStorageService', 'admin'])
            ->latest();

        return Grid::make($model, function (Grid $grid) {
            $grid->disableActions();
            $grid->disableCreateButton();
            $grid->disableRowSelector();

            $grid->column('bucket');
            $grid->column('objectStorageService.type');
            $grid->column('admin.name');
            $grid->column('result');
            $grid->column('created_at');
        });
    }
}
